package Test.sms;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import weaver.general.BaseBean;
import weaver.sms.SmsService;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.MessageDigest;
import java.util.HashMap;
import java.util.Map;
/**
 * @author fage
 * @since 2020-07-03  移动Mas平台短信对接
 */
public class SmsServiceImpl implements SmsService {

    @Override
    public boolean sendSMS(String smsId, String mobiles, String content) {
        BaseBean bb = new BaseBean();
        bb.writeLog("================start send msg================");
        Map map = new HashMap();

        String url = "http://112.35.10.201:5992/sms/norsubmit";
        String ecName = "徐州市云龙区人民政府";
        String apId = "ylqzfh";
        String secretKey = "Ecology_01";
        String sign = "lQDOcoEpw";
        String addSerial = "";


        StringBuffer sb = new StringBuffer();
        sb.append(ecName);
        sb.append(apId);
        sb.append(secretKey);
        sb.append(mobiles);
        sb.append(content);
        sb.append(sign);
        sb.append(addSerial);

        map.put("mac", MD5(sb.toString()));
        map.put("mobiles", mobiles);
        map.put("content", content);

        map.put("ecName", ecName);
        map.put("apId", apId);
        map.put("secretKey", secretKey);
        map.put("addSerial", addSerial);
        map.put("sign", sign);

        String json = JSON.toJSONString(map);
        try {
            String encode = json;
            String response = sendPost(url, encode);
            JSONObject jsonObject = JSON.parseObject(response);
            if ("true".equals(jsonObject.getString("success"))) {
                return true;
            }else{
                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
            bb.writeLog("------------短信接口出现异常---------" + e);
        }
        bb.writeLog("================end send msg================");
        return true;
    }



    public static void main(String[] args) {
        Map map = new HashMap();

        String url = "http://112.35.10.201:5992/sms/norsubmit";
        String ecName = "徐州市云龙区人民政府";
        String apId = "ylqzfh";
        String secretKey = "Ecology_01";
        String sign = "lQDOcoEpw";
        String mobiles = "18502502915";
        String content = "222";
        String addSerial = "";

        StringBuffer sb = new StringBuffer();
        sb.append(ecName);
        sb.append(apId);
        sb.append(secretKey);
        sb.append(mobiles);
        sb.append(content);
        sb.append(sign);
        sb.append(addSerial);

        System.out.println("================sb.toString================"+ sb.toString());

        map.put("mac", MD5(ecName+apId+secretKey+mobiles+content+sign+addSerial));
        map.put("mobiles", mobiles);
        map.put("content", content);

        map.put("ecName", ecName);
        map.put("apId", apId);
        map.put("secretKey", secretKey);
        map.put("addSerial", addSerial);
        map.put("sign", sign);

        System.out.println("================map================  "+ map);

        String json = JSON.toJSONString(map);
        System.out.println("================json================  "+ json);
        String encode = json;
        String response = sendPost(url, encode);
        JSONObject jsonObject = JSON.parseObject(response);

        System.out.println("================jsonObject================  "+ jsonObject);

        System.out.println(encode);
        System.out.println(jsonObject);

    }


    /**
     *
     * @Title: sendPost
     * @Description: Http post请求
     * @param @param url
     * @param @param param
     * @return String 返回类型
     * @throws
     */
    public static String sendPost(String url, String param) {
        OutputStreamWriter out = null;
        BufferedReader in = null;
        String result = "";
        try {
            URL realUrl = new URL(url);
            HttpURLConnection conn = null;
            // 打开和URL之间的连接
            conn = (HttpURLConnection) realUrl.openConnection();
            // 发送POST请求必须设置如下两行
            conn.setRequestMethod("POST"); // POST方法
            conn.setDoOutput(true);
            conn.setDoInput(true);
            // 设置通用的请求属性
            conn.setRequestProperty("accept", "*/*");
            conn.setRequestProperty("connection", "Keep-Alive");
            conn.setRequestProperty("user-agent",
                    "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            conn.setRequestProperty("Content-Type", "application/json");
            conn.connect();
            // 获取URLConnection对象对应的输出流
            out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
            // 发送请求参数
            out.write(param);
            // flush输出流的缓冲
            out.flush();
            // 定义BufferedReader输入流来读取URL的响应
            in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
            new BaseBean().writeLog("请求出现异常：" + e);
        }
        // 使用finally块来关闭输出流、输入流
        finally {
            try {
                if (out != null) {
                    out.close();
                }
                if (in != null) {
                    in.close();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        return result;
    }

    public static String MD5(String s) {
        char hexDigits[]={'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};

        try {
            byte[] btInput = s.getBytes("UTF-8");

            MessageDigest mdInst = MessageDigest.getInstance("MD5");
            mdInst.update(btInput);
            byte[] md = mdInst.digest();
            int j = md.length;
            char str[] = new char[j * 2];
            int k = 0;
            for (int i = 0; i < j; i++) {
                byte byte0 = md[i];
                str[k++] = hexDigits[byte0 >>> 4 & 0xf];
                str[k++] = hexDigits[byte0 & 0xf];
            }
            return new String(str);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }


}
